/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asseco.bazi2.controllers;

import com.asseco.bazi2.models.Klient;
import com.asseco.bazi2.models.Sifrarnik;
import com.asseco.bazi2.models.TipSifrarnik;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.asseco.mio.core.webapp.controller.BaseFormController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.asseco.mio.codex.controllers.*;
import org.springframework.validation.Validator;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import oracle.jdbc.OracleTypes;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Controller;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author Bojan
 */
@Controller
@RequestMapping("/novsifrarnikzakatalog*")
public class NovSifrarnikZaKatalog extends BaseFormController {

    @Autowired(required = false)
    Validator validator;

    @ModelAttribute(value = "sifrarnik")
    public Sifrarnik getSifrarnik() {

        return new Sifrarnik();

    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    protected Sifrarnik showForm(HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        String id = request.getParameter("id");

        String nivo = request.getParameter("nivo");

        String roditel = request.getParameter("roditel");



        if (!StringUtils.isBlank(id)) {

            Sifrarnik sifrarnik = new Sifrarnik();


            Configuration c = new Configuration().configure();
            SessionFactory sf = c.buildSessionFactory();
            Session session = sf.openSession();
            session.beginTransaction();

            Connection con = session.connection();  // obtain JDBC connection from Session object


            CallableStatement cs = con.prepareCall("{ call baza.SIFRARNIK_PACK.get_sifrarnik_po_id(?,?) }");

            cs.setInt(2, Integer.parseInt(id));

            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.execute();

            ResultSet resultSet = (ResultSet) cs.getObject(1);
            if (resultSet != null) {
                while (resultSet.next()) {

                    sifrarnik.setKod(resultSet.getString(1));
                    sifrarnik.setIme(resultSet.getString(2));
                    sifrarnik.setOpis(resultSet.getString(3));
                    sifrarnik.setTipSifrarnik(resultSet.getInt(4));
                    sifrarnik.setId(Integer.parseInt(id));

                }
                resultSet.close();
            }






            CallableStatement cs2 = con.prepareCall("{ call baza.SIFRARNIK_PACK.listanje_type_sif(?) }");



            cs2.registerOutParameter(1, OracleTypes.CURSOR);
            cs2.execute();

            List<TipSifrarnik> tempList = new ArrayList<TipSifrarnik>();

            ResultSet resultSet2 = (ResultSet) cs2.getObject(1);
            if (resultSet2 != null) {
                while (resultSet2.next()) {

                    TipSifrarnik temp = new TipSifrarnik();

                    temp.setId(resultSet2.getInt(1));

                    temp.setIme(resultSet2.getString(2));

                    tempList.add(temp);

                }
                resultSet2.close();
            }

            session.getTransaction().commit();
            session.close();
            sf.close();



            sifrarnik.setAllTipSifrarnik(tempList);






            if (sifrarnik.getIme() != null) {
                return sifrarnik;
            } else {

                saveMessage(request, "pogreshno id za sifrarnik!");

                return new Sifrarnik();

            }


        } else {
            return new Sifrarnik();
        }
    }

    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(Sifrarnik sifrarnik, BindingResult errors, HttpServletRequest request,
            HttpServletResponse response) throws SQLException {



        if (validator != null) { // validator is null during testing
            validator.validate(sifrarnik, errors);

            if (errors.hasErrors() && request.getParameter("delete") == null) { // don't validate when deleting
                return "novsifrarnik";
            }
        }




        Configuration c = new Configuration().configure();
        SessionFactory sf = c.buildSessionFactory();
        Session session = sf.openSession();
        session.beginTransaction();

        Connection con = session.connection();  // obtain JDBC connection from Session object

        // dodavanje_klient(kkod in varchar2, kime in varchar2, kprezime in varchar2, kadresa in varchar2, ktelefon in varchar2, kgrad_id in number, kout out varchar2)





        if (sifrarnik.getId() != null) {


            CallableStatement cs = con.prepareCall("{ call baza.SIFRARNIK_PACK.azuriranje_sifrarnik(?,?,?,?,?) }");

            cs.setInt(1, sifrarnik.getId());

            cs.setString(2, sifrarnik.getKod());

            cs.setString(3, sifrarnik.getIme());

            cs.setString(4, sifrarnik.getOpis());

            cs.setInt(5, sifrarnik.getTipSifrarnik());

            cs.execute();
            session.getTransaction().commit();
            session.close();
            sf.close();


            saveMessage(request, "Uspeshno e azuriran vnesot vo sifrarnik");


            return "redirect:mainMenu";
        } else {


            CallableStatement cs = con.prepareCall("{ call baza.SIFRARNIK_PACK.dodavanje_sifrarnik(?,?,?,?) }");



            cs.setString(1, sifrarnik.getKod());

            cs.setString(2, sifrarnik.getIme());

            cs.setString(3, sifrarnik.getOpis());

            cs.setInt(4, sifrarnik.getTipSifrarnik());

            cs.execute();
            session.getTransaction().commit();
            session.close();
            sf.close();


            saveMessage(request, "Uspeshno e dodaden sifrarnik");


            return "redirect:mainMenu";
        }

    }
}
